package model.workersWithData;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.GregorianCalendar;

import model.entities.DataBase;
import testpackage.Test;
import entities.IGarage;
import entities.IMaster;
import entities.IOrder;
import entities.OrderStatus;

public class DBManager {

	private ArrayList<IOrder> orderList = new ArrayList<IOrder>();
	private ArrayList<IMaster> masterList = new ArrayList<IMaster>();
	private ArrayList<IGarage> garageList = new ArrayList<IGarage>();
	private DataBase db;
	private final String SUCCESS= "Thanks, data was resave successfully";
	private static final String SORRY_BAN= "Sorry, this action is not allowed";
	private Test t= new Test();

	public DBManager() {
		db = new DataBase();
		masterList.addAll(db.getMasterList());
		garageList.addAll(db.getGarageList());
		orderList.addAll(db.getOrderList());

	}

	public void getEmptyGarage() {
		WorkerWithGarage.getEmptyGarage(garageList);
	}

	public void getOrdersByMaster(String masterName) {
		WorkerWithMaster.getOrdersByMaster(masterName, masterList);
	}

	public void getMastersByOrder(Integer numberOrder) {
		WorkerWithOrder.getMastersByOrder(numberOrder, orderList);
	}

	public void getNumberEmptyPlaces(GregorianCalendar date) {
		WorkerWithGarage.getNumberEmptyPlaces(masterList, garageList, date);
	}

	public void getNextFreeDate() {
		WorkerWithOrder.getNextFreeDate(orderList);
	}

	public void SortOrder(Comparator sortBy) {
		WorkerWithOrder.SortOrder(orderList, sortBy);
	}

	public void SortMaster(Comparator sortBy) {
		WorkerWithMaster.SortMaster(masterList, sortBy);
	}

	public void SortMadingOrder(Comparator sortBy) {
		WorkerWithOrder.getListMadingOrder(orderList, sortBy);

	}

	public void SortOrderByPeriod(OrderStatus ordSt, Comparator sortBy,
			GregorianCalendar begin, GregorianCalendar end) {
		WorkerWithOrder.getOrderByPeriod(orderList, sortBy, ordSt, begin, end);
	}

	public void addMaster(IMaster master) {
		WorkerWithMaster.addMaster(masterList, master);
	}

	public void deleteMaster(String masterName) {
		WorkerWithMaster.deleteMaster(masterList, masterName);
	}

	public void addGarage(IGarage garage) {
		if(t.getGarageManager()==true){
		WorkerWithGarage.addGarage(garageList, garage);
		}else{
			System.out.println(SORRY_BAN);
		}
	}

	public void deleteGarage(Integer numberGarage) {
		if(t.getGarageManager()){
		WorkerWithGarage.deleteGarage(garageList, numberGarage);
		}else{
			System.out.println(SORRY_BAN);
		}
	}

	public void addOrder(IOrder order, String masterName, Integer numberGarage) {
		WorkerWithOrder.addOrder(orderList, order, masterList, garageList,
				masterName, numberGarage);
	}

		
	public void markOrderDeleted(Integer numberOrder) {
		if(t.getPermissionDeleteOrder()==true){
		WorkerWithOrder.markOrder(orderList,OrderStatus.DELETED, numberOrder);
		}else{
			System.out.println(SORRY_BAN);
		}
	}

	public void markOrderCanceled(Integer numberOrder) {
		WorkerWithOrder.markOrder(orderList,OrderStatus.CANCELED, numberOrder);
	}

	public void markOrderMade(Integer numberOrder) {
		WorkerWithOrder.markOrder(orderList,OrderStatus.MADE, numberOrder);
	}
	
	
	public void ShirtTimeOrder(GregorianCalendar newDate, Integer numberOrder) {
		if(t.getPermissionShiftTimeOrder()){
		WorkerWithOrder.ShirtTimeOrder(orderList, newDate, numberOrder);
		}else{
			System.out.println(SORRY_BAN);
		}
	}
	
	public IOrder cloneOrder(Integer numberOrder){
		IOrder clone=null;
		for(IOrder o:orderList){
			if(o.getNumberOrder()==numberOrder){
				clone = WorkerWithOrder.cloneOrder(o);
			}
		}
		return clone;
	}
	

	public void saveData() {
		db.saveGarageListToBD(garageList);
		db.saveMasterListToBD(masterList);
		db.saveOrderListToBD(orderList);
		System.out.println(SUCCESS);
	}

}
